<template>
  <view class="container">
    <!-- 顶部标题 -->
    <view class="header">
      <text class="title">求职信息填写</text>
    </view>
    
    <!-- 左侧导航栏 -->
    <view class="sidebar">
      <view 
        class="nav-item" 
        :class="{ active: currentTab === 0 }" 
        @click="switchTab(0)"
      >
        个人信息
      </view>
      <view 
        class="nav-item" 
        :class="{ active: currentTab === 1 }" 
        @click="switchTab(1)"
      >
        求职需求
      </view>
    </view>
    
    <!-- 主内容区 -->
    <view class="main-content">
      <!-- 个人信息 -->
      <view v-if="currentTab === 0" class="form-content">
        <view class="form-item">
          <text class="label">名字</text>
          <input class="input" placeholder="请输入" v-model="personalInfo.name" />
        </view>
        <view class="form-item">
          <text class="label">年龄</text>
          <input class="input" placeholder="请输入" v-model="personalInfo.age" type="number" />
        </view>
        <view class="form-item">
          <text class="label">性别</text>
          <radio-group @change="(e) => personalInfo.gender = e.detail.value" class="radio-group">
            <label class="radio">
              <radio value="男" :checked="personalInfo.gender === '男'"/>
              <text>男</text>
            </label>
            <label class="radio">
              <radio value="女" :checked="personalInfo.gender === '女'"/>
              <text>女</text>
            </label>
          </radio-group>
        </view>
        <view class="form-item">
          <text class="label">当前状态</text>
          <radio-group @change="(e) => personalInfo.status = e.detail.value"  class="radio-group">
            <label class="radio">
              <radio value=0 :checked="personalInfo.status === 0"/>
              <text>在职</text>
            </label>
            <label class="radio">
              <radio value=1 :checked="personalInfo.status === 1"/>
              <text>无业</text>
            </label>
            <label class="radio">
              <radio value=2 :checked="personalInfo.status === 2"/>
              <text>在校</text>
            </label>
			<label class="radio">
			  <radio value=3 :checked="personalInfo.status === 3"/>
			  <text>离校</text>
			</label>
          </radio-group>
        </view>
		
		<!-- 学历选择（显示文本，存储对应数字） -->
		  <view class="form-item">
		    <text class="label">学历</text>
		    <picker 
		      class="picker" 
		      mode="selector" 
		      :range="degreeTextList"  
		      @change="handleDegreeChange"  
		    >
		      <view class="picker-view">
		        <!-- 显示选中的学历文本，默认提示 -->
		        {{ personalInfo.degree !== null ? getDegreeText() : '请选择学历' }}
		      </view>
		    </picker>
		  </view>
      </view>
      
      <!-- 求职需求（保持不变） -->
      <view v-if="currentTab === 1" class="form-content">
        <!-- 期望企业行业 -->
        <view class="form-item">
          <text class="label">期望企业行业</text>
          <picker 
            class="picker" 
            mode="selector" 
            :range="industryNames"  
            @change="setIndustry"  
          >
            <view class="picker-view">
              {{ jobInfo.industryId ? getIndustryName() : '请选择' }}
            </view>
          </picker>
        </view>
        
        <!-- 期望职位类型 -->
        <view class="form-item">
          <text class="label">期望职位类型</text>
          <picker 
            class="picker" 
            mode="selector" 
            :range="jobTypeNames"  
            @change="setJobType"  
          >
            <view class="picker-view">
              {{ jobInfo.jobTypeId ? getJobTypeName() : '请选择' }}
            </view>
          </picker>
        </view>
        
        <!-- 期望职位（依赖职位类型） -->
        <view class="form-item">
          <text class="label">期望职位</text>
          <picker 
            class="picker" 
            mode="selector" 
            :range="jobNames"  
            :disabled="!jobInfo.jobTypeId"  
            @change="setJob"
          >
            <view class="picker-view">
              {{ 
                !jobInfo.jobTypeId ? '请先选择职业类型' : 
                (jobInfo.jobId ? getJobName() : '请选择') 
              }}
            </view>
          </picker>
        </view>
        
        <!-- 期望薪资 -->
        <view class="form-item">
          <text class="label">期望薪资(月)</text>
          <input class="input" placeholder="请输入起始薪资" v-model.number="jobInfo.startSalary" type="number"/>
        </view>
        <view class="form-item">
          <text class="label">期望薪资(月)</text>
          <input class="input" placeholder="请输入最高薪资" v-model.number="jobInfo.endSalary" type="number"/>
        </view>
		
		<view class="form-item">
		  <text class="label">薪资支付方式</text>
		  <radio-group @change="(e) => jobInfo.payMethod = e.detail.value" class="radio-group">
		    <label class="radio">
		      <radio value=0 :checked="jobInfo.payMethod === 0"/>
		      <text>日结</text>
		    </label>
		    <label class="radio">
		      <radio value=1 :checked="jobInfo.payMethod === 1"/>
		      <text>周结</text>
		    </label>
		    <label class="radio">
		      <radio value=2 :checked="jobInfo.payMethod === 2"/>
		      <text>月结</text>
		    </label>
		    <label class="radio">
		      <radio value=3 :checked="jobInfo.payMethod === 3"/>
		      <text>小时工</text>
		    </label>
		  </radio-group>
		</view>
        
        <!-- 期望工作地点（省份） -->
        <view class="form-item">
          <text class="label">期望工作地点(省份)</text>
          <picker
            class="picker" 
            mode="selector" 
            :range="regionNames" 
            @change="setProvince"  
          >
            <view class="picker-view">
              {{ jobInfo.provinceId ? getProvinceName() : '请选择' }}
            </view>
          </picker>
        </view>
        
        <!-- 期望工作地点（市县） -->
        <view class="form-item">
          <text class="label">期望工作地点(市县)</text>
          <picker
            class="picker" 
            mode="selector" 
            :range="countyNames"  
            :disabled="!jobInfo.provinceId"  
            @change="setCounty"
          >
            <view class="picker-view">
              {{ 
                !jobInfo.provinceId ? '请先选择省份' : 
                (jobInfo.countyId ? getCountyName() : '请选择') 
              }}
            </view>
          </picker>
        </view>
      </view>
    </view>
    
    <!-- 底部按钮区（保持不变） -->
    <view class="footer">
      <button 
        class="prev-btn" 
        @click="prevTab" 
        v-if="currentTab > 0"
      >
        上一步
      </button>
      <button 
        class="next-btn" 
        @click="nextTab" 
        v-if="currentTab < 1"
      >
        下一步
      </button>
      <button 
        class="complete-btn" 
        @click="submitForm" 
        v-if="currentTab === 1"
      >
        完成
      </button>
    </view>
  </view>
</template>

<script>
import { getPositionTypeAndFirst, getJobsByTypeId, getCountiesByProvinceId,submitCareerExpectInfo } from '@/api/user.js';
import state from '../../store/state';
export default {
  data() {
    return {
      currentTab: 0,
      // 学历配置：文本与数字的映射关系（中专:1，按需求调整顺序和数字）
      degreeMap: [
        { text: '中学', value: 0 },
        { text: '高中', value: 1 },
        { text: '中专', value: 2 },  // 中专对应数字2（可根据需求修改为1）
        { text: '大专', value: 3 },
        { text: '本科', value: 4 },
        { text: '硕士', value: 5 },
        { text: '博士', value: 6 }
      ],
      // 提取学历文本列表，用于picker显示
      degreeTextList: [],
      // 后端原始数据（保持不变）
      jobTypeDOS: [],  
      jobDOS: [],      
      regionDOS: [],   
      countys: [],     
	  industryDOS: [],
      
      // 用于picker显示的名称数组（保持不变）
      jobTypeNames: [],  
      jobNames: [],      
      regionNames: [],   
      countyNames: [],   
	  industryNames: [],
      
      // 个人信息（学历字段改为存储数字，初始值null表示未选择）
      personalInfo: {
        name: '',
        age: '',
        gender: '',
        status: -1,
        degree: null  // 存储学历对应数字（如中专:2），初始未选择
      },
      
      // 求职需求（保持不变）
      jobInfo: {
        jobTypeId: '',    
        jobId: '',        
		payMethod:undefined,
        startSalary: null,
        endSalary: null,
        provinceId: '',   
        countyId: '',
		industryId:''
      }
    }
  },
  onLoad() {
    // 初始化学历文本列表（从degreeMap提取text）
    this.degreeTextList = this.degreeMap.map(item => item.text);
    
    // 初始化加载职位类型和省份数据（保持不变）
    getPositionTypeAndFirst().then(res => {
      if (res.code === 200) {
        this.jobTypeDOS = res.data.jobTypeDOS || [];
        this.regionDOS = res.data.regionDOS || [];
		this.industryDOS = res.data.industryDOS || [];
        this.jobTypeNames = this.jobTypeDOS.map(item => item.typeName);
        this.regionNames = this.regionDOS.map(item => item.regionName);
		this.industryNames = res.data.industryDOS.map(item => item.industryName);
      }
    })
  },
  methods: {
    // 切换标签页（保持不变）
    switchTab(index) {
      this.currentTab = index;
      uni.pageScrollTo({ scrollTop: 0, duration: 0 });
    },
	// 上一页（保持不变）
    prevTab() {
      if (this.currentTab > 0) {
        this.currentTab--;
        uni.pageScrollTo({ scrollTop: 0, duration: 0 });
      }
    },
    // 下一页（保持不变）
    nextTab() {
      if (!this.validatePersonalInfo()) return;
      this.currentTab++;
      uni.pageScrollTo({ scrollTop: 0, duration: 0 });
    },
    
    // 处理学历选择：根据选中索引获取对应数字，存储到personalInfo.degree
    handleDegreeChange(e) {
      const selectedIndex = e.detail.value;
      // 根据索引从degreeMap中获取对应数字
      this.personalInfo.degree = this.degreeMap[selectedIndex].value;
    },
    
    // 辅助方法：根据存储的学历数字，获取对应的文本（用于picker显示）
    getDegreeText() {
      const degreeItem = this.degreeMap.find(item => item.value === this.personalInfo.degree);
      return degreeItem ? degreeItem.text : '';
    },
    
    // 选择职位类型（保持不变）
    setJobType(e) {
      const selectedIndex = e.detail.value;
      const selectedType = this.jobTypeDOS[selectedIndex];
      this.jobInfo.jobTypeId = selectedType.typeId;
      
      getJobsByTypeId({jobTypeId:selectedType.typeId}).then(res => {
        if (res.code === 200) {
          this.jobDOS = res.data || [];
          this.jobNames = this.jobDOS.map(item => item.jobName);
          this.jobInfo.jobId = '';
        }
      });
    },
	
	// 选择行业（保持不变）
	setIndustry(e) {
	  const selectedIndex = e.detail.value;
	  const selectedIndustry = this.industryDOS[selectedIndex];
	  this.jobInfo.industryId = selectedIndustry.industryId;
	},
    
    // 选择职位（保持不变）
    setJob(e) {
      const selectedIndex = e.detail.value;
      const selectedJob = this.jobDOS[selectedIndex];
      this.jobInfo.jobId = selectedJob.jobId;
    },
    
    // 选择省份（保持不变）
    setProvince(e) {
      const selectedIndex = e.detail.value;
      const selectedProvince = this.regionDOS[selectedIndex];
      this.jobInfo.provinceId = selectedProvince.regionId;
      
      getCountiesByProvinceId({ProvinceId:selectedProvince.regionId}).then(res => {
        if (res.code === 200) {
          this.countys = res.data || [];
          this.countyNames = this.countys.map(item => item.regionName);
          this.jobInfo.countyId = '';
        }
      });
    },
    
    // 选择市县（保持不变）
    setCounty(e) {
      const selectedIndex = e.detail.value;
      const selectedCounty = this.countys[selectedIndex];
      this.jobInfo.countyId = selectedCounty.regionId;
    },
    
    // 辅助方法：获取职位类型名称（保持不变）
    getJobTypeName() {
      const type = this.jobTypeDOS.find(item => item.typeId === this.jobInfo.jobTypeId);
      return type ? type.typeName : '';
    },
	
	// 辅助方法：获取行业名称（保持不变）
	getIndustryName() {
	  const industryDO = this.industryDOS.find(item => item.industryId === this.jobInfo.industryId);
	  return industryDO ? industryDO.industryName : '';
	},
    
    // 辅助方法：获取职位名称（保持不变）
    getJobName() {
      const job = this.jobDOS.find(item => item.jobId === this.jobInfo.jobId);
      return job ? job.jobName : '';
    },
    
    // 辅助方法：获取省份名称（保持不变）
    getProvinceName() {
      const province = this.regionDOS.find(item => item.regionId === this.jobInfo.provinceId);
      return province ? province.regionName : '';
    },
    
    // 辅助方法：获取市县名称（保持不变）
    getCountyName() {
      const county = this.countys.find(item => item.regionId === this.jobInfo.countyId);
      return county ? county.regionName : '';
    },
    
	// 检验个人信息填写（学历校验改为判断是否为null）
    validatePersonalInfo() {
      if (!this.personalInfo.name) {
        uni.showToast({ title: '请输入姓名', icon: 'none' });
        return false;
      }
      if (!this.personalInfo.age || this.personalInfo.age < 16 || this.personalInfo.age > 70) {
        uni.showToast({ title: '年龄应在16-70岁之间', icon: 'none' });
        return false;
      }
      if (!this.personalInfo.gender) {
        uni.showToast({ title: '请选择性别', icon: 'none' });
        return false;
      }
      if (this.personalInfo.status === -1) {
        uni.showToast({ title: '请选择当前状态', icon: 'none' });
        return false;
      }
      // 学历校验：判断是否为null（未选择）
      if (this.personalInfo.degree === null) {
        uni.showToast({ title: '请选择学历', icon: 'none' });
        return false;
      }
      return true;
    },
    
    // 提交表单（学历参数改为传递数字）
    submitForm() {
      if (!this.validatePersonalInfo()) return;
      
      if (!this.jobInfo.jobId) {
        uni.showToast({ title: '请选择期望职位', icon: 'none' });
        return;
      }
      if (!this.jobInfo.startSalary || !this.jobInfo.endSalary || this.jobInfo.startSalary > this.jobInfo.endSalary) {
        uni.showToast({ title: '请输入有效的薪资范围', icon: 'none' });
        return;
      }
      if (!this.jobInfo.countyId) {
        uni.showToast({ title: '请选择期望市县', icon: 'none' });
        return;
      }
	  const jobInfo = {
		  userId:state.userInfo.userId,
		  payMethod:this.jobInfo.payMethod,
		  startSalary:this.jobInfo.startSalary,
		  maxSalary:this.jobInfo.endSalary,
		  industryId:this.jobInfo.industryId,
		  jobId:this.jobInfo.jobId,
		  regionId:this.jobInfo.countyId,
		  username:this.personalInfo.name,
		  age:this.personalInfo.age,
		  gender:this.personalInfo.gender,
		  jobSearchStatus:this.personalInfo.status,
		  degree: this.personalInfo.degree  // 提交学历对应数字（如中专:2）
	  }
      submitCareerExpectInfo(jobInfo).then(res=>{
		  if(res.code==200){
			  setTimeout(() => {
			      uni.navigateTo({
			      	url:'/pages/zcy/login'
			      })
			  }, 500);
		  }else{
			  uni.showToast({ title: '提交失败', icon: 'error' });
		  }
	  })
    }
  }
}
</script>

<style scoped>
/* 样式保持不变 */
.container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  background-color: #fff;
  position: relative;
}

.header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 20rpx 30rpx;
  border-bottom: 1rpx solid #eee;
}

.title {
  font-size: 36rpx;
  font-weight: bold;
}

.sidebar {
  width: 200rpx;
  background-color: #f5f9ff;
  position: absolute;
  left: 0;
  top: 100rpx;
  bottom: 120rpx;
}

.nav-item {
  padding: 30rpx;
  color: #666;
  border-left: 8rpx solid transparent;
  transition: all 0.3s ease;
}

.nav-item.active {
  color: #1890ff;
  border-left-color: #1890ff;
  background-color: #e6f7ff;
  font-weight: bold;
}

.main-content {
  margin-left: 220rpx;
  padding: 30rpx;
  flex: 1;
  overflow-y: auto;
}

.form-content {
  animation: fadeIn 0.3s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(10rpx); }
  to { opacity: 1; transform: translateY(0); }
}

.form-item {
  margin-bottom: 40rpx;
}

.label {
  display: block;
  margin-bottom: 20rpx;
  font-size: 30rpx;
  color: #333;
}

.input, .picker-view {
  width: 100%;
  height: 80rpx;
  border: 1rpx solid #ddd;
  border-radius: 8rpx;
  padding: 0 20rpx;
  box-sizing: border-box;
  line-height: 80rpx;
}

.picker {
  width: 100%;
}

.radio-group {
  display: flex;
  padding: 10rpx 0;
}

.radio {
  display: flex;
  align-items: center;
  margin-right: 40rpx;
  font-size: 28rpx;
}

.footer {
  height: 120rpx;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  padding: 0 30rpx;
  border-top: 1rpx solid #eee;
  gap: 20rpx;
}

.prev-btn, .next-btn, .complete-btn {
  border-radius: 8rpx;
  padding: 0 40rpx;
  height: 80rpx;
  line-height: 80rpx;
  font-size: 28rpx;
}

.prev-btn {
  background-color: #f5f5f5;
  color: #333;
}

.next-btn {
  background-color: #1890ff;
  color: #fff;
}

.complete-btn {
  background-color: #52c41a;
  color: #fff;
}
</style>